package com.aletto.general_store.ui;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class GoodsGridItemDecoration extends RecyclerView.ItemDecoration {

    private final int mSpace;

    public GoodsGridItemDecoration(int space){
        mSpace = space;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if(layoutManager instanceof GridLayoutManager){
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            int spanCount = gridLayoutManager.getSpanCount();
            int position = parent.getChildAdapterPosition(view); // 当前条目位置

            outRect.bottom = mSpace;
            if ((position + 1) % spanCount != 0){
                outRect.right = mSpace / 2;
            } else {
                outRect.left = mSpace / 2;
            }

        }
    }

}
